<template>
  <div class="home-container">
    <el-container class="home-container">
      <el-header>
        <!--头部区域-->
        <div>
          <a class="left-head" href="/login">
            <img class="icon" src="../assets/book.png" alt="图片不见了" />
          </a>
          <span>MMY Reader</span>
        </div>
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!--页面主区域-->
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '150px'">
          <!-- 侧边栏菜单曲 -->
          <!-- 或者 :unique-opened="true" -->
          <el-menu
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            unique-opened
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#409ffd"
            :default-active="activePath"
            ><!-- active-text-color 激活文本颜色 -->
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menulist"
              :key="item.id"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
                @click="saveNameState('/' + item2.path)"
              >
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ item2.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 返回的数据放在菜单上
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址，页面创建时赋值
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    // 保证刷新仍然高亮
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 保存连接的激活状态
    saveNameState(a) {
      window.sessionStorage.setItem('activePath', a)
      this.activePath = a // 保证切换仍然高亮
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message({
        showClose: true,
        message: '已退出'
      })
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus') // 简化promise操作使用await和async
      // const res = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data // 获取左侧菜单数据
    },
    // 点击按钮，切换菜单折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.icon {
  height: 50px;
  width: 50px;
  border-radius: 50%;
}
.el-header {
  background-color: antiquewhite;
  display: flex;
  justify-content: space-between; //两边布局
  padding-left: 0px;
  align-items: center;
  color: lightcoral;
  font-size: 20px;
  font-family: 'Lucida Handwriting', Papyrus;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    // 使得菜单栏右侧边框线对齐
    border-right: 0;
  }
}
.el-main {
  background-color: #cccccc;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: lightcoral;
  font-size: 20px;
  // line-height: 24px;
  // width: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; // 放鼠标成小手手
  font-family: Arial, 'Times New Roman';
}
</style>
